# -*- coding: utf-8 -*-
"""
@time：  2021/05/13  22:02
@Auto：Mr. xingye  993843278
@Company 星野训练营 99384378@qq.com
@Function:兼容不同的excel版本

"""
import os
from common.Logger import logger
from common.excel import OldExecl
from common.excel import NewExcel


def get_reader(srcfile=''):
    """
    获取读取excel的对象
    :param srcfile: excel文件路径
    :return: 读取excel的对象
    """
    reader = None
    # 如果打开的文件不存在，就报错
    if not os.path.isfile(srcfile):
        logger.error("%s not exist!" % (srcfile))
        return reader
    if srcfile.endswith(".xls"):
        reader = OldExecl.Reader()
        reader.open_excel(srcfile)
        return reader
    if srcfile.endswith(".xlsx"):
        reader = NewExcel.Reader()
        reader.open_excel(srcfile)
        return reader

def get_writer(srcfile,dstfile):
    """
    获取写入excel的对象
    :param srcfile: excel远文件路径
    :param dstfile: 写入后excel文件保存的路径
    :return: 写入excel的对象
    """
    writer = None
    # 如果打开的文件不存在，就报错
    if not os.path.isfile(srcfile):
        logger.error("%s not exist!" % (srcfile))
        return writer
    if srcfile.endswith('.xls'):
        writer = OldExecl.Writer()
        writer.copy_open(srcfile,dstfile)
        return writer
    if srcfile.endswith('.xlsx'):
        writer = NewExcel.Writer()
        writer.cope_open(srcfile,dstfile)
        return writer

if __name__ == '__main__':
    reader = get_reader('../../lib/webcase/cases/电商登录用例.xls')
    sheetname = reader.get_sheets()
    # print(sheetname)
    for sheet in sheetname:
        reader.set_sheet(sheet)
        lines = reader.readline()
        for i in range(len(lines)):
            print(lines[i])
        print()

    # writer = get_writer('../../lib/cases/电商登录用例.xlsx', '../../lib/cases/result-电商登录用例.xlsx')
    # sheetname = writer.get_sheets()
    # writer.set_sheet(sheetname[0])
    # writer.write(1, 1, 'William', 3)
    # writer.save_close()
